package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.admin.dto.LoginDto;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppJwtUtil;
import com.heima.media.entity.WmUser;
import com.heima.user.entity.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.IApUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * APP用户信息表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-07-28
 */
@Service
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements IApUserService {


    /**
     * APP用户登录    1.手机号密码进行登录  2.手机设备进行登录
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(LoginDto dto) {
        //1.判断用户登录方式
        if (StringUtils.isBlank(dto.getPhone())) {
            //设备登录   手机号空判定 只是设备登录
            Map<String, Object> claimMaps = new HashMap<>();
            claimMaps.put("userId", 0);
            claimMaps.put("equipmentId", dto.getEquipmentId());
            String token = AppJwtUtil.getToken(claimMaps);

            //4.响应业务数据 data:{user:{},token:""}
            HashMap<String, Object> mapResult = new HashMap<>();
            //用户信息进行脱敏处理
            mapResult.put("token", token);
            return ResponseResult.okResult(mapResult);
        } else {
            //手机号密码登录
            LambdaQueryWrapper<ApUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApUser::getPhone, dto.getPhone());
            ApUser one = this.getOne(queryWrapper);
            if (one == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
            }
            if (one.getStatus().equals(1)) {
                return ResponseResult.errorResult(400, "账号被锁定了");
            }
            //2.2 判断密码是否正确
            String pwdStr = dto.getPassword() + one.getSalt();
            String pwdEncodedStr = DigestUtils.md5DigestAsHex(pwdStr.getBytes());
            if (!pwdEncodedStr.equals(one.getPassword())) {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
            //3.生成令牌-JWT令牌载荷中包含社交用户信息：APP用户
            Map<String, Object> claimMaps = new HashMap<>();
            claimMaps.put("userId", one.getId());
            claimMaps.put("userName", one.getName());
            String token = AppJwtUtil.getToken(claimMaps);

            //4.响应业务数据 data:{user:{},token:""}
            HashMap<String, Object> mapResult = new HashMap<>();
            //用户信息进行脱敏处理
            one.setPassword(null);
            one.setPhone(null);
            one.setSalt(null);
            mapResult.put("user", one);
            mapResult.put("token", token);
            return ResponseResult.okResult(mapResult);
        }
    }
}
